package com.cloudcc.mobile.view.main.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.baidu.android.pushservice.PushManager;
import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.MenuToggleEvent;
import com.cloudcc.cloudframe.net.async.AsyncClient;
import com.cloudcc.mobile.AppConfig;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.AppManager;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.dao.impl.CommonEngineImpl;
import com.cloudcc.mobile.im.IMHelper;
import com.cloudcc.mobile.manager.CacheManager;
import com.cloudcc.mobile.manager.ConfigManager;
import com.cloudcc.mobile.manager.ContactsManager;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.service.DingWeiService;
import com.cloudcc.mobile.util.DialogUtils;
import com.cloudcc.mobile.view.activity.CloudccActivity;
import com.cloudcc.mobile.view.base.BaseFragment;
import com.cloudcc.mobile.view.main.SettingIMActivity;
import com.cloudcc.mobile.view.wel.GuideViewActivity;
import com.cloudcc.mobile.weight.DialogUtils.DialogOnClickListener;
import com.cloudcc.mobile.weight.SlideSwitch;
import com.cloudcc.mobile.weight.SlideSwitch.OnChangeListener;
import com.easemob.EMCallBack;

/**
 * 设置界面
 * 
 * @author seven
 * 
 */
public class SettingFragment extends BaseFragment implements OnChangeListener {
	@Bind(R.id.setmessage)
	SlideSwitch togglePush;// 推送
	@Bind(R.id.setshengyin)
	SlideSwitch togglePushSound;// 推送声音
	@Bind(R.id.setzhengdong)
	SlideSwitch togglePushVibrate;// 推送震动
	@Bind(R.id.setloaction)
	SlideSwitch toggleLocal;// 位置
	@Bind(R.id.timeset)
	TextView timeset;

	@Override
	public void init() {
		super.init();
		fillUI();
		addListener();
	}
	//初始化页面
	private void fillUI() {

		// #1 push
		togglePush.setSelected(ConfigManager.getInstance().isAllowPush());
		togglePushSound.setSelected(ConfigManager.getInstance()
				.isAllowPushSound());
		togglePushVibrate.setSelected(ConfigManager.getInstance()
				.isAllowPushVibrate());

		// location
		toggleLocal.setSelected(ConfigManager.getInstance().isAllowLocation());
		int locationTime = ConfigManager.getInstance().getLocationTime();
		switch (locationTime) {
		case 3600000:
			timeset.setText("1小时");
			break;
		case 7200000:
			timeset.setText("2小时");
			break;
		case 3*60*60*1000:
			timeset.setText("3小时");
			break;
		}
	}
	@OnClick(R.id.guanyu)
	public void clickAboutMe() {
		Intent intent = new Intent(getActivity(), CloudccActivity.class);
		startActivity(intent);
	}

	@OnClick(R.id.weixin)
	public void clickSettingIM() {
		 Intent intent = new Intent(getActivity(), SettingIMActivity.class);
		 startActivity(intent);
	}

	@OnClick(R.id.huancun)
	public void clickClearCache() {
		DialogUtils.showClearCache(mContext);
	}

	@OnClick(R.id.exit)
	public void clickExit() {
		DialogUtils.Exit(mContext);
	}

	@OnClick(R.id.zhuxiao)
	public void clickLoginOut() {
		showClearLogin(mContext);
	}

	@OnClick(R.id.menu_setting)
	public void menuTogggle() {
		EventEngine.post(new MenuToggleEvent(false, false));
	}

	@OnClick(R.id.xiaoxi)
	public void clickPushContainer() {
		togglePush.setSelected(togglePush.isSelected());
	}

	@OnClick(R.id.shengyin)
	public void clickPushSoundContainer() {
		togglePushSound.setSelected(togglePushSound.isSelected());
	}

	@OnClick(R.id.zhendong)
	public void clickPushVibrateContainer() {
		togglePushVibrate.setSelected(togglePushVibrate.isSelected());
	}

	@OnClick(R.id.weizhi)
	public void clickLoactionContainer() {
		toggleLocal.setSelected(toggleLocal.isSelected());
	}

	@OnClick(R.id.cloudcc_version)
	public void clickCheckViersion() {// 版本检测
		EventEngine.register(this);
		new CommonEngineImpl().checkUpdate();
	}


	@OnClick(R.id.time)
	public void clickLoactionTimeContainer() {
		final AlertDialog dialog = new AlertDialog.Builder(mContext).create();
		final View view = LayoutInflater.from(mContext).inflate(
				R.layout.timesetting, null);
		dialog.setView(view);
		final RadioGroup radioGroup = (RadioGroup) view
				.findViewById(R.id.timesetting);
		String time = timeset.getText().toString().trim();
		if (time.equals("1小时")) {
			radioGroup.check(R.id.radio4);
		} else if (time.equals("2小时")) {
			radioGroup.check(R.id.radio5);
		} else if (time.equals("3小时")) {
			radioGroup.check(R.id.radio6);
		}
		final Button queren = (Button) view.findViewById(R.id.queren);
		final Button quxiao = (Button) view.findViewById(R.id.quxiao);
		quxiao.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});
		queren.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				RadioButton radioButton = (RadioButton) view
						.findViewById(radioGroup.getCheckedRadioButtonId());
				String timeText = radioButton.getText().toString().trim();
				timeset.setText(timeText);
				int times = AppConfig.LOCATION_TIME;
				if (timeText.equals("1小时")) {
					times = 1*60*60*1000;
				} else if (timeText.equals("2小时")) {
					times = 2*60*60*1000;
				} else if (timeText.equals("3小时")) {
					times = 3*60*60*1000;
				}
				ConfigManager.getInstance().setLocationTime(times);
				Intent intent = new Intent(getActivity(), DingWeiService.class);
				if (toggleLocal.isSelected()) {
					AppContext.isStopLocation = false;
					getActivity().stopService(intent);
					getActivity().startService(intent);
				}
				dialog.cancel();
			}
		});
		dialog.show();
	}

	/*
	 * 获取控件
	 */
	private void addListener() {
		togglePush.setOnChangeListener(this);// 消息监听
		togglePushSound.setOnChangeListener(this);// 声音监听
		togglePushVibrate.setOnChangeListener(this);// 震动监听
		toggleLocal.setOnChangeListener(this);
	}

	/**
	 * 注销登录
	 * 
	 * @param context
	 */
	public void showClearLogin(final Context context) {
		com.cloudcc.mobile.weight.DialogUtils.showWarn(mContext, "注销账户", "亲！确认注销吗？", new DialogOnClickListener() {
			@Override
			public void onClickPositive(final Dialog dialog) {
				//退出环信
				IMHelper.getInstance().logout(false, new EMCallBack() {
					
					@Override
					public void onSuccess() {
						getActivity().runOnUiThread(new Runnable() {
							public void run() {
								loginOut();
								
							}
						});
					}
					@Override
					public void onProgress(int arg0, String arg1) {
						
					}
					
					@Override
					public void onError(int arg0, String arg1) {
						dialog.cancel();
					}
				});

			}
			@Override
			public void onClickNegative(Dialog dialog) {
				dialog.cancel();
			}
		});
	}
	
	private void loginOut(){
		CacheManager.getInstance().clear();
		UserManager.getManager().clearUser();
		RunTimeManager.getInstance().clearUserCache();
		ContactsManager.getInstance().clearContactCache();
		AsyncClient.getInstance().clear(mContext);
		Intent intent = new Intent(mContext,
				GuideViewActivity.class);
		intent.putExtra("page.index", 5);
		mContext.startActivity(intent);
		getActivity().finish();
		AppManager.getInstance().AppExit();
	}

	@Override
	public int getLayoutId() {
		return R.layout.setting2;
	}
	
	//SlideSwitch变化监听
	@Override
	public void onChange(SlideSwitch sb, boolean state) {
		ConfigManager configManager = ConfigManager.getInstance();
		switch (sb.getId()) {
		case R.id.setmessage:// push
			if (state) {
				PushManager.resumeWork(getActivity());
			} else {
				PushManager.stopWork(getActivity());
			}
			configManager.setPush(state);
			break;
		case R.id.setshengyin:// push
			configManager.setAllowPushSound(state);
			break;
		case R.id.setzhengdong:// push
			configManager.setAllowPushVibrate(state);
			break;
		case R.id.setloaction:// locat
			Intent intent = new Intent(getActivity(), DingWeiService.class);
			if (state) {
				AppContext.isStopLocation = false;
				getActivity().startService(intent);
			} else {
				AppContext.isStopLocation = true;
				getActivity().stopService(intent);
			}
			configManager.setlocation(state);
			break;

		default:
			break;
		}
	}

}
